// 配置
var id = {
    form: 'common_role_form',
    table: 'common_role_table',
}
var editIndex = null;
var common_role = {
    form: {
        id: id.form,
        form: [
            {code: 1, type: 'text', field: 'name', title: '角色名称', placeholder: '请输入角色名称'},
            {code: 1, type: 'text', field: 'code', title: '角色编码', placeholder: '请输入角色编码'},
            {code: 8, field: 'image', title: '图片'},
            {code: 9, field: 'mImage', title: '多图片'},
            {code: 10, field: 'file', title: '文件'},
            {code: 1, type: 'number', field: 'level', title: '角色等级', placeholder: '1-9数字越小权限等级越大'},
            {
                code: 2, title: '是否默认', items: [
                    {field: 'isDefault', value: '0', title: '非默认', type: 'radio'},
                    {field: 'isDefault', value: '1', title: '默认', type: 'radio'}
                ]
            },
            {code: 7, field: 'remark', title: '内容', placeholder: '请输入内容信息', config: {height: '200px'}, editIndex: null}
        ]
    },
    table: {
        id: id.table,
        url: api.system.module,
        cols:[
            {field: 'name', title: '模块名称'},
            {field: 'code', title: '模块编码'},
            {field: 'orderNumber', title: '排序', minWidth: 60},
            {fixed: 'right', title: '操作', toolbar: '<div>' +
                    '<a class="layui-btn layui-btn-xs" lay-event="update">update</a>' +
                    '<a class="layui-btn layui-btn-xs" lay-event="delete">delete</a>' +
                    '</div>'}
        ],
        toolbar: '<div class="layui-btn-container">' +
            '<button class="layui-btn layui-btn-sm" lay-event="create">创建</button>' +
            '</div>',
        bar: function (bar) {
            switch (bar.event) {
                case 'create':
                    common_role_view.form();
                    break;
            }
        },
        tool: function (tool) {
            switch (tool.event) {
                case 'edit':
                    common_role_view.form(tool.data)
                    break;
                case 'delete':
                    common_role_view.delete(tool.data)
                    break;
            }
        },
        info: {
            height: '310px',
            data: []
        }
    }
}

// 初始化
initialize({
    title: '角色权限管理',
    module: ['layedit', 'upload', 'common_upload'],
    init: function () {
        soda.table.render(common_role.table);
    }
})

// 表单视图
var common_role_view = {
    form: function (_data) {
        app.form({
            title: '角色管理',
            height: '460px',
            width: '800px',
            finish: function () {
                soda.table.reload(id.table);
            },
            init: function () {
                soda.form.render(common_role.form);
                soda.convert.formatFormData(0, common_role.form.form, _data ? _data : {});
                if (_data) {
                    soda.form.set(id.form, _data);
                } else {
                    soda.form.set(id.form, {isDefault: 0});
                }
           },
            opt: function () {
                var form = soda.form.get(id.form);
                soda.convert.formatFormData(0, common_role.form.form, form, true)

                var v = soda.form.verify(form, id.form, ['name', 'code'])
                if (!v.check) {
                    soda.tips.warning(v.message);
                    return false
                }

                form.image = $('#' + id.form).find('img').attr('src');

                if (form.id) {
                    common_role_data.update(form).done(function (d) {
                        soda.tips.ok(d.message)
                        admin.finishForm();
                    })
                } else {
                    form.systemId = get_system_id();
                    common_role_data.create(form).done(function (d) {
                        soda.tips.ok(d.message)
                        admin.finishForm();
                    })
                }
            }
        })
    },
    delete: function (_data) {
        soda.tips.confirm({
            text: '确定要删除这条数据吗?',
            confirm: function () {
                common_role_data.delete([_data.id]).done(function (d) {
                    soda.tips.ok(d.message);
                    table.reload(id.table, {});
                });
            }
        });
    },
    set_default: function (_id) {
        soda.tips.confirm({
            text: '是否要调整默认值操作?',
            confirm: function () {
                common_role_data.set_default(_id).done(function (d) {
                    soda.tips.ok(d.message);
                    table.reload(id.table, {});
                });
            }
        });
    }
}

// 数据业务逻辑
var common_role_data = {
    create: function (_form) {
        var param = {
            url: api.system.role,
            data: _form
        }
        return soda.http.post(param);
    },
    update: function (_form) {
        var param = {
            url: api.system.role + '/' + _form.id,
            data: _form
        }
        return soda.http.put(param);
    },
    delete: function (_id) {
        var param = {
            url: api.system.role,
            data: {ids: _id}
        }
        return soda.http.delete(param);
    },
    set_default: function (_id) {
        var param = {
            url: api.system.role + '/default/' + _id
        }
        return soda.http.put(param);
    }
}

// 操作控制
var common_role_opt = {
    set_default: function (_id) {
        common_role_view.set_default(_id)
    }
}

function delete_file(data, dom) {
    $(dom).parent().parent().remove();

}

function delete_img(data, dom) {
    $(dom).parent().remove();

}